﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco;
using umbraco.NodeFactory;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.web;
using System.Xml;

namespace ScDataTypes
{

    public static class Functions
    {

        /// <summary>
        /// Get a document set from a path
        /// </summary>
        /// <param name="Id">Id of the current document</param>
        /// <param name="Location">Location path to navigate to example /index/products or ../other </param>
        /// <returns></returns>
        public static Document[] GetDocumentFromPath(int Id, string Location)
        {
            // Notes: This is a quick and dirty approch need revisiting

            try
            {
                // Get inital node
                if (Location.Length != 0)
                {
                    if (umbraco.helper.IsNumeric(Location))
                    {
                        Document objDocument = new Document(Convert.ToInt32(Location));
                        if (objDocument != null && objDocument.Id == Convert.ToInt32(Location))
                            return new Document[] { objDocument };
                    }
                    else
                    {
                        Document objDocument = null;
                        List<string> colLocation = new List<string>();
                        colLocation.AddRange(Location.Split("/".ToCharArray()));

                        // Get the base documemt to path search on
                        if (Location.StartsWith("/"))
                        {
                            Document[] arrRootDocuments = Document.GetRootDocuments();

                            if (colLocation.Count == 1)
                            {
                                return new Document[] { arrRootDocuments[0] };
                            }
                            else
                            {
                                bool blnFound = false;

                                foreach (Document objRootDocument in arrRootDocuments)
                                    if (string.Compare(objRootDocument.Text, colLocation[1], true) == 0)
                                    {
                                        blnFound = true;
                                        objDocument = objRootDocument;
                                        break;
                                    }

                                // Not not found
                                if (blnFound == false) return new Document[] { };
                            }

                            // Remove the first node
                            if (colLocation.Count != 0) colLocation.RemoveAt(0);
                            if (colLocation.Count != 0) colLocation.RemoveAt(0);
                        }
                        else
                            objDocument = new Document(Convert.ToInt32(Id));

                        //  Loop through the entire path an find the correct document
                        if (objDocument != null &&
                            Location.Trim().Length != 0)
                        {
                            foreach (string PathItem in colLocation)
                            {
                                // Found flag for pathitem part
                                bool blnFound = false;

                                if (PathItem == "..")
                                {
                                    if (objDocument.ParentId == 0)
                                        return new Document[] { };
                                    else
                                    {
                                        blnFound = true;
                                        objDocument = new Document(objDocument.ParentId);
                                    }
                                }
                                else
                                    foreach (Document objChildDocuments in objDocument.Children)
                                    {
                                        if (string.Compare(objChildDocuments.Text, PathItem, true) == 0)
                                        {
                                            blnFound = true;
                                            objDocument = objChildDocuments;
                                            break;
                                        }
                                    }

                                // Not not found
                                if (blnFound == false) return new Document[] { };
                            }

                            return objDocument.Children;
                        }

                    }
                }
                else
                {
                    return  new Document(Id).Children;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }

            return new Document[] { };
        }

    }

}
